<?php
namespace Ucmaim\Controller\Admin;
use Think\Controller;

class AuthController extends Controller{
    private static $session;

    public function _initialize(){
        self::$session = 'ADMIN';
    }

    public function Login(){
        if( IS_POST ){
            $post = I('post.', '', 'trim');
            if(!$this->_verifycode($post['verify'], true)) $this->error('验证码不正确');
            $info = D('Admin')->getManager($post['admin_name'], $post['admin_pass']);
            if( !empty($info) ){
                $this->_saveSession($info);
                $this->redirect('Admin/Index');
            }else{
                $this->error('用户名或密码不正确。', U('Admin/Auth/Login'));
            }
            exit();
        }

        $this->display();
    }

    public function Logout(){
        $name = self::$session;
        session($name, null);
        $this->redirect('Admin/Auth/Login');
    }

    public static function getAuthSessionName(){
        return self::$session;
    }

    public function ajax_verifycode(){
        $code = I('post.verify');
        $this->ajaxReturn($this->_verifycode($code, false));
    }

    private function _verifycode($code, $reset = true){
        $config['reset'] = true === $reset ? true : false;
        $Verify = new \Think\Verify($config);
        $result =  $Verify->check($code);
        return $result;
    }

    private function _saveSession($info){
        $name = self::$session;
        session($name, $info);
    }
}

